package com.geen.tuoke.utils.ext


import android.widget.EditText
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.widget.Toolbar
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.geen.tuoke.R
import com.qmuiteam.qmui.kotlin.onClick


/**
 * 初始化普通的toolbar 只设置标题
 */
fun Toolbar.setCenterTitleAndRightImg(titleStr: String? = "",res:Int=0,onRight: (() -> Unit)?=null): Toolbar {
    findViewById<TextView>(R.id.tv_toolbar_title).text = titleStr
    findViewById<ImageView>(R.id.iv_right).run {
        setImageResource(res)
        onClick {
            onRight?.invoke()
        }
    }
    return this
}


/**
 * 初始化有返回键的toolbar
 */
fun Toolbar.initClose(
    titleStr: String? = "",
    backImg: Int = R.mipmap.ic_back,
    onBack: (toolbar: Toolbar) -> Unit
): Toolbar {
    initMenu(titleStr,backImg,onBack)
    return this
}


fun Toolbar.initMenu(
    titleStr: String? = "",
    backImg: Int = R.mipmap.ic_back,
    onBack: (toolbar: Toolbar) -> Unit,
    rightImg: Int = 0,
    onRight: (() -> Unit)?=null
): Toolbar {
    setCenterTitleAndRightImg(titleStr,rightImg,onRight)
    setNavigationIcon(backImg)
    setNavigationOnClickListener { onBack.invoke(this) }
    return this
}

//绑定普通的Recyclerview
fun RecyclerView.init(
    layoutManger: RecyclerView.LayoutManager,
    bindAdapter: BaseQuickAdapter<*, *>,
    isScroll: Boolean = true
): RecyclerView {
    layoutManager = layoutManger
    setHasFixedSize(true)
    adapter = bindAdapter
    isNestedScrollingEnabled = isScroll
    return this
}

 fun showEditFocus(editText: EditText,delay:Boolean = false) {
     if(delay){
         editText.postDelayed({
             editText.isEnabled = true
             editText.isFocusable = true
             editText.isFocusableInTouchMode = true
             editText.requestFocus()
             editText.isCursorVisible = true
         },200)
     }else{
         editText.isEnabled = true
         editText.isFocusable = true
         editText.isFocusableInTouchMode = true
         editText.requestFocus()
         editText.isCursorVisible = true
     }
}